package com.txm.drawPic;
/*
* 子弹
*
* */
import java.util.Vector;

public class Bullet extends Thread {
    Tank tank;
    private int x;
    private int y;
    private final int width = 1;
    private final int height = 1;
    private String direct;

    public boolean isLive() {
        return isLive;
    }

    private boolean isLive = true;

    public void setLive(boolean live) {
        isLive = live;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    private int speed = 6;

    public Bullet(int x, int y, String direct, Tank tank) {
        this.x = x;
        this.y = y;
        this.direct = direct;
        this.tank = tank;
    }

    @Override
    public void run() {
        while (isLive) {
            if(Tank.MYPANEL != null && Tank.MYPANEL.gameOver) return;
//            System.out.println(Thread.currentThread().getName() + "发射子弹" + x + " " + y);
            if (direct.equals("1")) {
                y -= speed;
            }
            if (direct.equals("2")) {
                x += speed;
            }
            if (direct.equals("3")) {
                y += speed;
            }
            if (direct.equals("4")) {
                x -= speed;
            }
            if (tank instanceof UsTank) {
                Vector<EnemyTank> enemyTanks = ((UsTank) tank).getEnemyTank();
                for (EnemyTank enemyTank : enemyTanks) {
                    isHit(enemyTank);
                }
            } else if (tank instanceof EnemyTank) {
                isHit(((EnemyTank) tank).getEnemyTank());
            }

            if (0 > x || x > Tank.PANEL_W || 0 > y || y > Tank.PANEL_H) {
                isLive = false;
                break;
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    //  判断子弹是否命中tank
    public void isHit(Tank tank) {
        if (!tank.isLive()) return;
        String direct = tank.getDirect();
        if (direct.equals("1") || direct.equals("3")) {
            int tankW = tank.getX() + tank.getMain_width() + 2 * tank.getTrack_width();
            if (x >= tank.getX() && x <= tankW && y >= tank.getY() && y <= tank.getY() + tank.getTrack_height()) {
                this.isLive = false;
                tank.setLive(false);
                System.out.println(direct + "-子弹" + Thread.currentThread().getName() + "-命中" + tank.getMark());
//                System.out.println(MyPanel.Bombs);
            }
        } else {
            int tankY = tank.getY() + tank.getMain_width() + 2 * tank.getTrack_width();
            if (x >= tank.getX() && x <= tank.getX() + tank.getTrack_height() && y >= tank.getY() && y <= tankY) {
                this.isLive = false;
                tank.setLive(false);
                System.out.println(direct + "-子弹" + Thread.currentThread().getName() + "-命中" + tank.getMark());
//                System.out.println(MyPanel.Bombs);
            }
        }

    }
}